﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;

namespace Scryber.Testing.AllSystemTests
{
    public class CustomBindings
    {
        public CustomBindings()
        { }

        //
        // methods for the object datasource
        //

        public static XPathNavigator GetByLastNameStatic(string lastname)
        {
            BookList list = GetStandardBooks();
            BookList matched = new BookList();

            foreach (Book a in list)
            {
                if (a.Author.LastName.Equals(lastname, StringComparison.OrdinalIgnoreCase))
                    matched.Add(a);
            }
            return GetNavigator(matched);
        }

        public XPathNavigator GetAll()
        {
            BookList list = GetStandardBooks();
            return GetNavigator(list);
        }

        public System.Xml.XmlDocument GetBetweenIDs(int min, int max, bool inclusive)
        {
            BookList list = GetStandardBooks();
            BookList matched = new BookList();
            if (inclusive)
            {
                min -= 1;
                max += 1;
            }
            foreach (Book a in list)
            {
                if (a.ID > min && a.ID < max)
                    matched.Add(a);
            }
            return GetDocument(matched);
        }

        public BookList MatchWithTitleRegEx(string regex)
        {
            BookList list = GetStandardBooks();
            BookList matched = new BookList();
            System.Text.RegularExpressions.Regex matcher = new System.Text.RegularExpressions.Regex(regex);

            foreach (Book b in list)
            {
                if (matcher.IsMatch(b.Title))
                    matched.Add(b);
            }

            return matched;
        }

        //
        // conversion methods
        //

        private static XPathNavigator GetNavigator(BookList books)
        {
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(BookList));
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                ser.Serialize(ms, books);
                ms.Flush();
                ms.Position = 0;
                XPathDocument doc = new XPathDocument(ms);
                XPathNavigator nav = doc.CreateNavigator();

                return nav;
            }
        }

        private static System.Xml.XmlDocument GetDocument(BookList books)
        {
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(BookList));
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                ser.Serialize(ms, books);
                ms.Flush();
                ms.Position = 0;
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(ms);
                return doc;
            }
        }

        //
        // standard books methods
        //

        private static BookList GetStandardBooks()
        {
            BookList parsed = new BookList();
            string[] all = list.Split('\n');
            foreach (string line in all)
            {
                string[] parts = line.Split('\t');
                int id = int.Parse(parts[0].Trim());
                string title = parts[1].Trim();
                string authfull = parts[2].Trim();
                string authlast = authfull.IndexOf(' ') > 0 ? authfull.Substring(authfull.LastIndexOf(' ') + 1) : authfull;
                string authfirst;
                if (authfull.Length == authlast.Length)
                    authfirst = string.Empty;
                else
                    authfirst = authfull.Substring(0, authfull.Length - authlast.Length).Trim();

                Book b = new Book() { ID = id, Title = title };
                b.Author = new Author() { FullName = authfull, LastName = authlast, OtherNames = authfirst };
                parsed.Add(b);
            }

            return parsed;
        }



        const string list = @"01	My experiments with Truth	Mahatma M.K. Gandhi
02	Far from the Madding Crowd	Thomas Hardy
03	Geetanjali	Rabindra Nath Tagore
04	One Day in the Life of Ivan Denisovitch	Alexander Solzhenitsyn
05	The Merchant of venice	William shakespeare
06	The Moon and Six pense	Somerset Maughan
07	Pilgrim's Progress from this world to that which is to come	John Bunyan
08	A Tale of Two Cities	Charles Dickens
09	Utopia	Sir Thomas Moor
10	Origin of species	charles Darwin
11	David Copperfield	Charles Dickens
12	A passage to India	E.M. Forster
13	Gulliver's Travels	Jonathan Swift
14	Discovery of India	Pandit Jawaharlal Nehru
15	The Vicar of Wakefield	Oliver Goldsmith
16	The Decline and Fall of the Roman Empire	Edward Gibbon
17	The Lady of the Last Minstrel	Sir Walter Scott
18	Pride and Prejudice	Jane Austen
19	Time Machine	H.G. Wells
20	Arthashastra	Kautilya
21	Le Contract Social	Jean Jacques Rousseau
22	Avigyan Sakuntalam	Kalidas
23	Anand Math	Bankimchandra Chattopadhyay
24	Mein Kampf	Adolf Hitler
25	Ain-i-Akbari	Abul Fazal
26	Akbar-Nama	Abul Fazal
27	Shakuntala	Kalidas
28	War and peace	Leo Tolstoy
29	A Dangerous place	D.P. Moynihan
30	Raghuvamsa	Kalidas
31	Adventures of Sherlock Holmes	Arthur Conan Doyle
32	Adventures of Tom Saweyer	Mark Twain
33	Agni Veena	Kazi Nasrul Islam
34	Alice in Wonderland	Lewis Carrol
35	Ancient Mariner	Coleridge
36	Animal Farm	George Orwell
37	Anna Karenina	Tolstoy
38	Antony and Cleopatra	Shakespeare
39	Arms and the Man	G.B. Shaw
40	Around the World in eighty days	Jules Verne
41	Baburnama	Babur
42	Ben Hur	Lewis Wallace
43	Bhagwat Gita	Ved Vyas
44	Bisarjan	R.N. Tagore
45	Canterbury Tales	Chaucer
46	Chitra	R.N. Tagore
47	Count of Monte Cristo	Alexander Dumas
48	Crime and Punishment	Dostoevsky
49	Das Kapital	Karl Marx
50	Divine Comedy	Dante
51	Dr.Jekyll and Mr.Hyde	Stevenson
52	Don Quixote	Cervantes
53	Dr.Zhivago	Boris Pasternak
54	For whom the Bell Tolls	Ernest Hemingway
55	Forsyte Saga	John Galsworthy
56	Freedom at Midnight	Dominique Lapierre
57	Gathering Storm	Winston Churchill
58	Geet Govind	Jaya Dev
59	Gora	R.N. Tagore
60	Hamlet	Shakespeare
61	Harsha Charit	Bana Bhatt
62	Hunchback of Notre Dame	Victor Hugo
63	Hungry Stones	R.N. Tagore
64	Illiad	Homer
65	Invisible Man	H.G. Wells
66	Ivanhoe	Walter Scott
67	Jungle Book	Rudyard Kipling
68	Kadambari	Bana Bhatt
69	Kidnapped	R.L. Stevenson
70	King Lear	Shakespeare
71	Kumar Sambhav	Kalidas
72	Last Days of Pompeii	Bulwar Lytton
73	Les Miserable	Victor Hugo
74	Life Divine	Shri Aurobindo
75	Mahabharata	Vyas
76	Man and Superman	G.B. Shaw
77	Meghdut	Kalidas
78	Mother	Maxim Gorky
79	Odyssey	Homer
80	Oliver Twist	Charles Dickens
81	Othello	Shakespeare
82	Panchatantra	Vishnu Sharma
83	Paradise lost	John Milton
84	Pickwick Papers	Charles Dickens
85	Plague	Albert Camus
86	The Post Office	R.N. Tagore
87	Principia	Issac Newton
88	Ramayana	Valmiki
89	Robinson Crusoe	Daniel Defoe
90	Shah Nama	Firdausi
91	Shape of things to come	H.G. Wells
92	Three Musketeers	Alexander Dumas
93	The Tempest	Shakespeare
94	Tom Sawyer	Mark Twain
95	Treasure Island	R.L. Stevenson
96	Ulysses	James Joyce
97	Uncle Tom's Cabin	Mrs.Harriet Stowe
98	Waste Land	T.S. Eliot
99	Nineteen Eighty-four	George Orwell
100	Sunny Days	Sunil Gavaskar
101	Faust	Goethe
102	Arabian Nights	Sir Richard Burton
103	The City of Joy	Dominique Lapierre
104	The One Day Wonders	Sunil Gavaskar
105	Silas Marner	George Eliot
106	Bachelor of Arts	R.K. Narayan
107	China Passage	John Kenneth Galbraith
108	A Suitable Boy	Vikram Seth
109	A Voice For Freedom	Nayantara Saigal
110	A Week with Gandhi	Louis Fisher
111	A Woman's Life	Guy de Maupassaut
112	Age of Reason	Jean Paul Sartre
113	Asian Drama	Gunnar Myrdal
114	The Bubble	Mulk Raj Anand
115	Ben Hur	Lewis Wallace
116	The Castle	Franz Kalka
117	Chandalika	Rabindra Nath Tagore
118	The Class	Erich Byron
119	The Clown	Heinrich Boll
120	Comedy of Errors	William Shakespeare
121	Communist Manifesto	Karl Marx
122	Confessions	Jean Jacques Rousseau
123	The Court Dancer	Rabindra Nath Tagore
124	Death of a City	Amrita Pritam
125	Decline and Fall of the Roman	Edward Gibbon Empire
126	Essays of Gita	Sir Aurobindo Ghosh
127	French Revolution	Thomas Carlyle
128	Ganadevata	Tara Shankar Bandopadhyaya
129	Glimpses of World History	Jawaharlal Nehru
130	The Godfather	Mario Puzo
131	Grammar of Politics	Harold T. Laski
132	Guide	R.K. Narayan
133	Hindu View of Life	Dr.S.Radha Krishnan
134	Hungry Stones	Rabindra Nath Tagore
135	India Divided	Dr.Rajendra Prasad
136	Jurassik Park	Michael Crichton
137	Kidnapped	Robert Louis Stevenson
138	Richard Nixon	Leaders
139	Mahatma Gandhi	Romain Rolland
140	The Masters	C.P. Shaw
141	My Truth	Indira Gandhi
142	Old Man and the Sea	Earnest Hemingway
143	The Other Side of Midnight	Sindye Sheldon
144	Pride and Prejudice	Jane Austen
145	Shape of Things to Come	H.G. Wells
146	Sons and Lovers	D.H. Lawrence
147	Treasure Island	R.L. Stevenson
148	Valley of Dolls	Jacqueline Susann
149	Wealth of Nations	Adam Smith";

         
    }


    [Serializable()]
    public class Book
    {
        [System.Xml.Serialization.XmlAttribute("id")]
        public int ID { get; set; }

        public string Title { get; set; }

        public Author Author { get; set; }

    }

    [System.Xml.Serialization.XmlRoot("Books",Namespace="http://schemas.scryber.co.uk/TopBookList")]
    public class BookList : List<Book>
    {
    }

    [Serializable]
    public class Author
    {
        public string OtherNames { get; set; }

        public string LastName { get; set; }

        public string FullName { get; set; }
    }



}
